package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsOrderMapper;
import com.qf.mapper.IndexTotalMapper;
import com.qf.pojo.DtsOrder;
import com.qf.vo.CategorySellAmts;
import com.qf.vo.DayStatis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private DtsOrderMapper orderMapper;
    @Autowired
    private IndexTotalMapper indexTotalMapper;



    public int countOrder() {
        QueryWrapper<DtsOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("deleted", false);
        Integer count = orderMapper.selectCount(queryWrapper);
        return count;
    }

    @Override
    public List<CategorySellAmts> categorySellCount() {
        List<CategorySellAmts> list = indexTotalMapper.categorySellCount();
        return list;
    }

    @Override
    public List<DayStatis> orderAmts() {
        List<DayStatis> list = indexTotalMapper.orderAmts();
        return list;
    }

    @Override
    public IPage<DtsOrder> queryByPage(Integer id, Integer userId,
                                      Integer page, Integer limit,
                                      String sort, String order) {
        IPage<DtsOrder> OrderIPage = new Page<>(page, limit);

        QueryWrapper<DtsOrder> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("deleted",false);

        if (!StringUtils.isEmpty(id)){
            queryWrapper.eq("id",id);
        }

        if (!StringUtils.isEmpty(userId)){
            queryWrapper.eq("userId",userId);
        }

        //排序
        if ("asc".equalsIgnoreCase(order)) {
            queryWrapper.orderByAsc(sort);
        } else {
            queryWrapper.orderByDesc(sort);
        }

        IPage<DtsOrder> dtsOrderIPage = orderMapper.selectPage(OrderIPage, queryWrapper);

        return dtsOrderIPage;
    }

    @Override
    public DtsOrder queryById(Integer id) {

        DtsOrder dtsOrder = orderMapper.selectById(id);
        return dtsOrder;
    }
}
